package com.gitee.huanminabc.utils_tools.class_extend.javassist.cut;

import com.gitee.huanminabc.utils_tools.class_extend.javassist.ClassAspect;
import javassist.ClassPool;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class ClassAspectTestCut implements ClassAspect {
    @Override
    public void before(Class<?> targetClass, String methodName, Object[] args) {
        log.info("before->targetClass:" + targetClass.getName());
        log.info("before->methodName: " + methodName);
        log.info("before->args: " + Arrays.toString(args));
    }

    @Override
    public void after(Class<?> targetClass, String methodName, Object[] args, Object result) {
        log.info("after->targetClass:" + targetClass.getName());
        log.info("after->methodName: " + methodName);
        log.info("after->args: " + Arrays.toString(args));
        log.info("after->result: " + result);

    }

    @Override
    public void afterReturning(Class<?> targetClass, String methodName, Object[] args, Object result) {
        log.info("afterReturning ->targetClass:" + targetClass.getName());
        log.info("afterReturning ->methodName: " + methodName);
        log.info("afterReturning ->args: " + Arrays.toString(args));
        log.info("afterReturning ->result: " + result);

    }

    @Override
    public void afterThrowing(Class<?> targetClass, String methodName, Object[] args, Throwable e) {
        log.info("afterThrowing ->targetClass:" + targetClass.getName());
        log.info("afterThrowing ->methodName: " + methodName);
        log.info("afterThrowing ->args: " + Arrays.toString(args));
        log.info("afterThrowing ->e: " + e);
    }

    @Override
    public String modifyMethodBody(ClassPool cPool) {
        cPool.importPackage("lombok.extern.slf4j.Slf4j");
        return "{" +
                    "log.info(\"modifyMethodBody\"); " +
                    "return \"hello\";" +
                "}";
    }
}
